#include "neuron.hpp"


Neuron::Neuron() {
    m_nConn = 0;
    m_connections = 0;
    m_threshold = 0;
    m_state = 0;
}

Neuron::~Neuron() {
    delete[] m_connections;
}

void Neuron::init (uint nConn, uint threshold) {
    m_nConn = nConn;
    m_connections = new connection [m_nConn];
    m_threshold = threshold;
}

void Neuron::setState (float state) {
    m_state = state;
}

float Neuron::getState () {
    return m_state;
}

void Neuron::copy (Neuron * source) {
    if (m_nConn != source->m_nConn) {
        delete[] m_connections;
        m_nConn = source->m_nConn;
        m_connections = new connection [source->m_nConn];
    }
    for (uint i = 0; i < m_nConn; i++) {
        m_connections[i] = source->m_connections[i];
    }
}
void Neuron::mutate (int rate) {
    m_threshold += rand() % rate - rate/2;
    for (uint i = 0; i < m_nConn; i++) {
        m_connections[i].weight += rand() % rate - rate/2;
    }
}

void Neuron::compute(){
    for (uint i=0; i<m_nConn; i++){
        m_state += m_connections[i].pNeuron->getState() * m_connections[i].weight;
    }
}

connection * Neuron::getConnections(){
    return m_connections;
}
